package actionbean;

import org.apache.commons.lang.StringUtils;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;
import controller.webservice.WebServiceCtrl;

/**
 * @author jaslin.wee.2012, huangjm.2012
 * 
 */
@UrlBinding(BaseActionBean.WEBSERVICE_PREFIX + "{$event}")
public class WebServiceActionBean implements ActionBean {

	private static final int JSON_INDENT_FACTOR = 4;

	private ActionBeanContext context;
	private String r;
	private WebServiceCtrl wsCtrl = new WebServiceCtrl();

	@Override
	public void setContext(ActionBeanContext context) {
		this.context = context;
	}

	@Override
	public ActionBeanContext getContext() {
		return context;
	}

	public String getR() {
		return r;
	}

	public void setR(String r) {
		this.r = r;
	}

	@DefaultHandler
	public Resolution defaultHandler() {
		String resource = StringUtils.removeStart(getContext().getRequest()
				.getRequestURI(), getContext().getRequest().getContextPath())
				+ "/";
		resource = StringUtils.removeStart(resource,
				BaseActionBean.WEBSERVICE_PREFIX);
		resource = StringUtils.removeStart(resource, "/");
		if (resource.isEmpty()) {
			return new RedirectResolution(BaseActionBean.URI_FOR_ADMIN_HOMEPAGE);
		}

		return new StreamingResolution("text",
				"ehhh. donno what to do bro ?!? ");
	}

	@HandlesEvent("homepage")
	public Resolution homepage() {
		return new ForwardResolution("/admin/homepage.jsp");
	}

	@HandlesEvent("dump")
	public Resolution dump() {
		// Set standard HTTP/1.1 no-cache headers.
		getContext().getResponse().setHeader("Cache-Control",
				"private, no-store, no-cache, must-revalidate");

		// Set standard HTTP/1.0 no-cache header.
		getContext().getResponse().setHeader("Pragma", "no-cache");

		return new StreamingResolution("application/json", wsCtrl
				.processDumpTableRequest().toString(JSON_INDENT_FACTOR));
	}

	@HandlesEvent("user-dump")
	public Resolution dumpUser() {
		// Set standard HTTP/1.1 no-cache headers.
		getContext().getResponse().setHeader("Cache-Control",
				"private, no-store, no-cache, must-revalidate");

		// Set standard HTTP/1.0 no-cache header.
		getContext().getResponse().setHeader("Pragma", "no-cache");

		return new StreamingResolution("application/json", wsCtrl
				.processDumpUserRequest(r).toString(JSON_INDENT_FACTOR));
	}

	@HandlesEvent("section-dump")
	public Resolution dumpSection() {
		// Set standard HTTP/1.1 no-cache headers.
		getContext().getResponse().setHeader("Cache-Control",
				"private, no-store, no-cache, must-revalidate");

		// Set standard HTTP/1.0 no-cache header.
		getContext().getResponse().setHeader("Pragma", "no-cache");

		return new StreamingResolution("application/json", wsCtrl
				.processDumpSectionRequest(r).toString(JSON_INDENT_FACTOR));
	}

	@HandlesEvent("bid-dump")
	public Resolution dumpBid() {
		// Set standard HTTP/1.1 no-cache headers.
		getContext().getResponse().setHeader("Cache-Control",
				"private, no-store, no-cache, must-revalidate");

		// Set standard HTTP/1.0 no-cache header.
		getContext().getResponse().setHeader("Pragma", "no-cache");

		return new StreamingResolution("application/json", wsCtrl
				.processDumpBidRequest(r).toString(JSON_INDENT_FACTOR));
	}

	@HandlesEvent("update-bid")
	public Resolution updateBid() {
		// Set standard HTTP/1.1 no-cache headers.
		getContext().getResponse().setHeader("Cache-Control",
				"private, no-store, no-cache, must-revalidate");

		// Set standard HTTP/1.0 no-cache header.
		getContext().getResponse().setHeader("Pragma", "no-cache");

		return new StreamingResolution("application/json", wsCtrl
				.processUpdateBidRequest(r).toString(JSON_INDENT_FACTOR));
	}

	@HandlesEvent("delete-bid")
	public Resolution deleteBid() {
		// Set standard HTTP/1.1 no-cache headers.
		getContext().getResponse().setHeader("Cache-Control",
				"private, no-store, no-cache, must-revalidate");

		// Set standard HTTP/1.0 no-cache header.
		getContext().getResponse().setHeader("Pragma", "no-cache");

		return new StreamingResolution("application/json", wsCtrl
				.processDeleteBidRequest(r).toString(JSON_INDENT_FACTOR));
	}

	@HandlesEvent("drop-section")
	public Resolution dropSection() {
		// Set standard HTTP/1.1 no-cache headers.
		getContext().getResponse().setHeader("Cache-Control",
				"private, no-store, no-cache, must-revalidate");

		// Set standard HTTP/1.0 no-cache header.
		getContext().getResponse().setHeader("Pragma", "no-cache");

		return new StreamingResolution("application/json", wsCtrl
				.processDropSectionRequest(r).toString(JSON_INDENT_FACTOR));
	}

	@HandlesEvent("start")
	public Resolution startRound() {
		// Set standard HTTP/1.1 no-cache headers.
		getContext().getResponse().setHeader("Cache-Control",
				"private, no-store, no-cache, must-revalidate");

		// Set standard HTTP/1.0 no-cache header.
		getContext().getResponse().setHeader("Pragma", "no-cache");

		return new StreamingResolution("application/json", wsCtrl
				.processStartRoundRequest().toString(JSON_INDENT_FACTOR));
	}

	@HandlesEvent("stop")
	public Resolution stopRound() {
		// Set standard HTTP/1.1 no-cache headers.
		getContext().getResponse().setHeader("Cache-Control",
				"private, no-store, no-cache, must-revalidate");

		// Set standard HTTP/1.0 no-cache header.
		getContext().getResponse().setHeader("Pragma", "no-cache");

		return new StreamingResolution("application/json", wsCtrl
				.processStopRoundRequest().toString(JSON_INDENT_FACTOR));
	}

}
